package com.logic.landseaserver.models.changerent.ws;

import com.logic.common.enums.HttpCodeEnum;
import com.logic.common.exception.BusinessException;
import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.landseaserver.common.LandeaConstants;
import com.logic.landseaserver.common.exception.LandseaBusinessException;
import com.logic.landseaserver.common.exception.LandseaException;
import com.logic.landseaserver.models.changerent.service.IChangeRentService;
import com.logic.landseaserver.service.IOffRent;
import com.logic.landseaserver.ws.dto.OffRentContractDTO;
import com.logic.landseaserver.ws.request.CreateOffLineContractsReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.HashMap;

/**
 * Created by Junny on 2017/12/5.
 * 转租
 */
@Path("/changeRent")
@Api(value="changeRent")
public class ChangeRentRest {

    @Autowired
    private IChangeRentService changeRentService;

    @Autowired
    private IOffRent offRentService;

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @POST
    @Path("/createChangeRent")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "新增转租合同", position = 1)
    public Response createReRent(@ApiParam(value = "param", required = true) CreateOffLineContractsReq req) {
        CommonResultMap map = new CommonResultMap();
        try {
            map.setData(changeRentService.createChangeRent(req));
            map.setResult(CommonResult.SUCCESS, "createChangeRent success");
        } catch (LandseaException e) {
            logger.error("createChangeRent LandseaException error", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getErrorDesc());
        } catch (BusinessException e) {
            logger.error("createChangeRent error", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        } catch (Exception e) {
            logger.error("createChangeRent error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @GET
    @Path("/getChangeContract/{contractId}")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "获取转租合同信息，contractId->A合同")
    public Response getReRentContract(
            @ApiParam(value = "param", required = true) @PathParam(value = "contractId") Integer contractId) {
        CommonResultMap map = new CommonResultMap();
        try {
            map.setData(changeRentService.queryChangeContract(contractId));
            map.setResult(CommonResult.SUCCESS);
        } catch (Exception e) {
            logger.error("ReRent || getReRentContract error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }


    @GET
    @Path("/getChangeRent/{contractId}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "根据A合同id查询转租申请")
    public Response getOffRentByContractId(
            @ApiParam(value = "param", required = true) @PathParam("contractId") Integer contractId){
        CommonResultMap map = new CommonResultMap();
        try {
            OffRentContractDTO offRentcontractDTO = offRentService.getOffRent(contractId, LandeaConstants.ReRentType.CHANGE);
            map.setData(offRentcontractDTO);
            map.setResult(CommonResult.SUCCESS);
        } catch (Exception e) {
            logger.error("changeRent || getChangeRent error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @POST
    @Path("/createPreBill/{contractId}")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "新增续租结转单")
    public Response createOverList(
            @ApiParam(value = "param", required = true) @PathParam(value = "contractId") Integer contractId) {
        CommonResultMap map = new CommonResultMap();
        try {
            changeRentService.createPreBill(contractId);
            map.setResult(CommonResult.SUCCESS);
        }catch (LandseaException e) {
            logger.error("ReRent || createOverList error.", e);
            map.setResult(CommonResult.ERROR, e.getErrorDesc());
        } catch (Exception e) {
            logger.error("ReRent || createOverList error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @DELETE
    @Path("/cancelChangeRent/{contractId}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "取消转租 contractId->A合同编号")
    public Response cancelReRent(
            @ApiParam(value = "param", required = true) @PathParam(value = "contractId") Integer contractId){
        CommonResultMap map = new CommonResultMap();
        try {
            HashMap<String,Object> result = new HashMap<>();
            result.put("result", changeRentService.cancleChangeRent(contractId));
            map.setData(result);
            map.setResult(CommonResult.SUCCESS);
        } catch (LandseaException e) {
            logger.error("changeRent || cancelChangeRent error.", e);
            map.setResult(CommonResult.ERROR, e.getErrorDesc());
        } catch (Exception e) {
            logger.error("changeRent || cancelChangeRent error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @POST
    @Path("/sysMoneyFor")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "余额结转至B租客", position = 1)
    public Response sysMoneyFor(@ApiParam(value = "param", required = true) @QueryParam("contractId") Integer contractId,
                                @QueryParam("userBId") Integer userBId) {
        CommonResultMap map = new CommonResultMap();
        try {
            changeRentService.sysMoney(contractId,userBId);
            map.setResult(CommonResult.SUCCESS, "sysMoneyFor success");
        } catch (LandseaException e) {
            logger.error("sysMoneyFor error", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        } catch (Exception e) {
            logger.error("sysMoneyFor error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

}
